/* eslint-disable no-param-reassign, max-params, max-lines */
import * as Vue from 'vue';
import { message } from 'ant-design-vue';
import AvicBpmFormCode from '@/views/avic/bpm/flowdetail/components/bpm-form-code/index.vue';
import { AvicBpmDetailList } from '../flowdetail/components/bpm-detail-list/index.js';
import { settings } from '@/settings';
import {
  start,
  getProcessDetailParameter,
  finishreader,
  getDefineIdByEntryId,
  getBpmButtonListBySelf,
  getoperateright,
  getOperaterightAndFormSecuritys,
  getBpmTabs,
  getBpmNavbarType,
  getProcessPicInfo
} from '@/api/avic/bpm/FlowUtilApi';
import { useAppStore } from '@/store/app';
import cloneDeep from 'lodash.clonedeep';
import core from '@/core';
import { default as router } from '@/router';
// function Store() {
//   return useAppStore();
// }
// import AvicBpmModal from '@/views/avic/bpm/flowdetail/components/bpm-modal/index.vue';
const AvicBpmModal = defineAsyncComponent(() => import('@/views/avic/bpm/flowdetail/components/bpm-modal/index.vue'));
let doc = null;
let beterScrollTop = 0;
/**
 * 流程通用工具类
 */

const bpmUtils = {
  /**
   * 根路径
   */
  baseurl: '/bpm/bpm',
  /**
   * 流程审批页面打开方式，可通过系统参数“PLATFORM_V6_BPMSHOWMODE”进行配置
   * default : window.open 的方式
   * dialog : top.layer.open 的方式
   * tab : tap.addTab 的方式
   * 默认不配置或配置不合法，按“default”处理
   */
  bpm_show_mode: 'default',
  // 弹窗实例
  bpm_modal_instance: null,
  /**
   * 判断非空
   *
   * @returns {Boolean}
   */
  notNull(...args) {
    for (let i = 0; i < args.length; i++) {
      const obj = args[i];
      if (typeof obj === 'undefined' || obj == null || (typeof obj === 'string' && obj === 'undefined')) {
        return false;
      }
      if (typeof obj === 'string' && obj.trim() === '') {
        return false;
      }
      if (obj === 'null') {
        return false;
      }
    }
    return true;
  },
  /**
   * 判断路由
   * @param dataJson
   */
  getConditions(dataJson) {
    return {
      // 分支
      isBranch() {
        if (dataJson.nextTask != null && dataJson.nextTask.length > 1) {
          // 分支
          return true;
        }
        // 非分支
        return false;
      },
      // 选人方式
      isUserSelectTypeAuto(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.userSelectType) {
          if (dataJson.nextTask[branchNo].currentActivityAttr.userSelectType === 'auto') {
            return true; // 自动选人方式
          }
          return false;
        }
        return false; // 手动选人方式
      },
      // 是否启用工作移交
      isWorkHandUser(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.isWorkHandUser) {
          if (dataJson.nextTask[branchNo].currentActivityAttr.isWorkHandUser === 'no') {
            return false; // 启用
          }
          return true; // 不启用
        }
        // 默认
        return true; // 不启用
      },
      // 处理方式
      getDealType(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.dealType) {
          return dataJson.nextTask[branchNo].currentActivityAttr.dealType;
        }
        return '2';
      },
      // 处理方式
      getSingle(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.single) {
          return dataJson.nextTask[branchNo].currentActivityAttr.single;
        }
        return 'no';
      },
      // 是否必须选人
      isMustUser(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.isMustUser) {
          if (dataJson.nextTask[branchNo].currentActivityAttr.isMustUser === 'no') {
            // 必须选人
            return false;
          }
          return true; // 必须选人
        }
        return true; // 默认值
      },
      // 是否启用密级
      isSecret(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.isSecret) {
          if (dataJson.nextTask[branchNo].currentActivityAttr.isMustUser === 'yes') {
            // 启用
            return true;
          }
          return false; // 不启用
        }
        return false; // 不启用
      },
      // 意见添写方式
      getIdeaType() {
        if (dataJson.currentActivityAttr.ideaType) {
          return dataJson.currentActivityAttr.ideaType;
        }
        return '';
      },
      // 是否强制表态
      isIdeaCompelManner() {
        if (dataJson.currentActivityAttr.ideaCompelManner) {
          if (dataJson.currentActivityAttr.ideaCompelManner === 'yes') {
            // 强制
            return true;
          }
          return false; // 不强制
        }
        return false; // 不强制
      },
      // 退回意见是否必填
      isNeedIdea() {
        if (dataJson.currentActivityAttr.isNeedIdea) {
          if (dataJson.currentActivityAttr.isNeedIdea === 'no') {
            // 强制
            return false;
          }
          return true; // 不强制
        }
        return true; // 不强制
      },
      // 是否显示选人框
      isSelectUser(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.isSelectUser) {
          if (dataJson.nextTask[branchNo].currentActivityAttr.isSelectUser === 'no') {
            // 不显示
            return false;
          }
          return true; // 显示
        }
        return true; // 显示
      },
      /**
       * 是否自动获取用户
       */
      isAutoGetUser(branchNo) {
        if (typeof branchNo === 'undefined') {
          branchNo = 0;
        }
        if (dataJson.nextTask != null && dataJson.nextTask[branchNo].currentActivityAttr.isAutoGetUser) {
          if (dataJson.nextTask[branchNo].currentActivityAttr.isAutoGetUser === 'no') {
            // 不显示
            return false;
          }
          return true; // 是
        }
        return false;
      },
      // 获取下节点类型
      getNextActivityType() {
        return dataJson.activityType;
      },
      // 是否是自由流程
      isUflow() {
        return dataJson.isUflow === '2';
      }
    };
  },
  /**
   * 封装 openFlowDetail方法，提供对象式API
   * @param {Object} options 打开流程详情页的参数
   */
  openFlowDetailByOptions(options) {
    return this.openFlowDetail(
      options.url,
      options.flowDetailUrl,
      options.callback,
      options.entryId ? `FlowDetail${options.entryId}` : '',
      options.viewMode,
      options.entryId,
      options.bpmOperatorRefresh
    );
  },
  // 打开流程详情页面
  // eslint-disable-next-line max-params
  openFlowDetail: async (
    url,
    flowDetailUrl,
    callback,
    windowName = 'NewFlowDetailWin',
    viewMode,
    entryId,
    bpmOperatorRefresh
    // eslint-disable-next-line consistent-return
  ) => {
    if (url) {
      let skipUrl = '';
      if (flowDetailUrl && url.indexOf('?') !== -1) {
        // eslint-disable-next-line prefer-destructuring
        url = url.split('?')[1];
        skipUrl = `${encodeURIComponent(flowDetailUrl.replace(RegExp('/', 'g'), '_'))}?${url}`;
      } else {
        if (url.indexOf('?') !== -1) {
          const urlStr = url.split('?');
          skipUrl = `${encodeURIComponent(urlStr[0].replace(RegExp('/', 'g'), '_'))}?${urlStr[1]}`;
        } else {
          skipUrl = encodeURIComponent(url.replace(RegExp('/', 'g'), '_'));
        }
      }

      const appStore = useAppStore();
      const bpmShowMode = await appStore.SET_BPMSHOWMODE(); // 先暂时写死
      bpmUtils.bpm_show_mode = bpmShowMode;
      if (bpmShowMode === 'dialog') {
        const params = bpmUtils.getQueryArr(skipUrl);
        // eslint-disable-next-line import/namespace
        bpmUtils.bpm_modal_instance = openBpmModal({
          modalTitle: viewMode === 'detailForm' ? '响应界面' : '详情',
          params,
          viewMode
        });
      } else if (bpmShowMode === 'tab') {
        bpmUtils.openFlowDetailWithTab({ skipUrl, windowName });
      } else {
        // 地址中有微前端地址
        let query = {};
        if (skipUrl.indexOf('microAppUrl') >= 0) {
          query = bpmUtils.getQueryArr(skipUrl);
        }
        if (bpmOperatorRefresh && typeof bpmOperatorRefresh === 'function') {
          window[`bpmOperatorRefresh${entryId}`] = () => {
            bpmOperatorRefresh();
          };
        }
        let base = settings.routerOptions?.base || '';
        if (base && base.endsWith('/')) {
          base = base.substring(0, base.length - 1);
        }
        // 兼容子应用地址结尾斜杠
        let microAppUrl = query.microAppUrl || '';
        if (microAppUrl && microAppUrl.endsWith('/')) {
          microAppUrl = microAppUrl.substring(0, microAppUrl.length - 1);
        }
        let newUrl = `${microAppUrl || base}/flowdetail/${skipUrl}`;
        const beforeOpenFlowWindow = settings?.beforeOpenFlowWindow || '';
        if (beforeOpenFlowWindow) {
          newUrl = beforeOpenFlowWindow(newUrl);
        }
        window.open(newUrl, windowName);
      }

      if (callback) {
        return callback();
      }
    } else {
      if (callback) {
        return callback();
      }
    }
  },
  openFlowDetailWithTab({ skipUrl, windowName }) {
    const params = bpmUtils.getQueryArr(skipUrl);
    const route = {
      path: `/${windowName}/:id/:type`, // TODO
      name: windowName,
      component: () => import('@/views/avic/bpm/flowdetail/index.vue'),
      meta: { title: params?.title || '流程详情' }
    };
    router.addRoute('RouterView', route);
    router.push(`/${windowName}/${skipUrl.split('?')[0]}/tab?${skipUrl.split('?')[1]}`);
  },
  getQueryArr(url) {
    // 如果链接没有参数，或者链接中不存在我们要获取的参数，直接返回空
    if (url.indexOf('?') === -1) {
      return;
    }

    // 获取链接中参数部分
    const queryString = url.substring(url.indexOf('?') + 1);

    // 分离参数对 ?key=value&key2=value2
    const parameters = queryString.split('&');

    const params = {};
    parameters.forEach((elem) => {
      const san = elem.split('=');
      if (san.length > 1) {
        const [key, value] = san;
        params[key] = value;
      }
    });

    if (url.indexOf('eform') === -1) {
      const routeId = url.split('?');
      params.routeId = routeId[0] ? routeId[0] : '';
    }
    // eslint-disable-next-line consistent-return
    return params;
  },
  // 普通流程添加打开流程页面 timestamp ,发起流程的当前窗口的唯一标识时间戳
  // 发起流程接口返回微前端信息
  // eslint-disable-next-line max-params
  addListStart(defineId, title, viewCode, timestamp) {
    const promise = new Promise((resolve, reject) => {
      start({ defineId })
        .then((res) => {
          if (res.success) {
            resolve(res);
            if (res.data.formType && res.data.formType === 'eformvue') {
              // 电子表单流程
              bpmUtils.openFlowDetail(
                `${res.data.formUrl}&viewCode=${
                  // 流程详情页url加入视图编码
                  viewCode
                }&defineId=${res.data.defineId}&deploymentId=${res.data.deploymentId}&title=${title}&timestamp=${timestamp}`,
                '',
                null,
                `FlowAdd-${res.data.defineId}-${res.data.deploymentId}`
              );
            } else {
              // 微前端-子应用表单
              if (res.data?.siteType === '1') {
                // eslint-disable-next-line max-len
                const detailUrl = `${res.data.formUrl}?defineId=${res.data.defineId}&deploymentId=${res.data.deploymentId}&title=${title}&timestamp=${timestamp}&siteType=${res.data.siteType}&microAppUrl=${res.data.microAppUrl}&microAppName=${res.data.microAppName}`;
                bpmUtils.openFlowDetail(detailUrl, '', null, `FlowAdd-${res.data.defineId}-${res.data.deploymentId}`);
              } else {
                bpmUtils.openFlowDetail(
                  // eslint-disable-next-line max-len
                  `${res.data.formUrl}?defineId=${res.data.defineId}&deploymentId=${res.data.deploymentId}&title=${title}&timestamp=${timestamp}`,
                  '',
                  null,
                  `FlowAdd-${res.data.defineId}-${res.data.deploymentId}`
                );
              }
            }
          } else {
            reject(res);
          }
        })
        .catch((err) => {
          resolve({ success: false, error: err });
        });
    });
    return promise;
  },
  detailByOptions(options) {
    return this.detail(
      options.bpmInstanceObject,
      options.formId,
      options.flowDetailUrl,
      options.extType,
      options.viewCode,
      options.bpmOperatorRefresh,
      options.viewMode,
      options.formId,
      options.formResourceName,
      options.customStatus
    );
  },
  /**
   * 获取流程详情页面相关数据
   * id 流程实例Id或者业务表单Id
   */
  // eslint-disable-next-line max-params
  detail: (
    bpmInstanceObject,
    id,
    flowDetailUrl,
    extType,
    viewCode,
    bpmOperatorRefresh,
    viewMode,
    formId,
    formResourceName,
    customStatus
  ) => {
    const promises = new Promise((resolve, reject) => {
      // if (!bpmUtils.notNull(extType)) {
      //   extType = '2';
      // }
      const postParam = {
        id
      };
      if (bpmUtils.notNull(extType)) {
        postParam.manager = extType;
      } else if (window.$config.flow?.defaultExtType) {
        postParam.manager = window.$config.flow.defaultExtType;
      }
      getProcessDetailParameter(postParam)
        .then((res) => {
          if (res.success) {
            if (res.data.length === 0) {
              if (viewMode === 'detailForm') {
                bpmUtils.executeTaskDetail(
                  id, // processInstanceId
                  '', // executionId
                  '', // taskID
                  formId, // formId
                  formResourceName, // formResourceName
                  '', // 展示流程标题
                  '', // formResourceName
                  flowDetailUrl,
                  bpmOperatorRefresh,
                  viewCode,
                  viewMode,
                  customStatus
                );
              } else {
                message.error('非本人参与流程，无法查看详情');
              }
            } else if (res.data.length === 1) {
              const nar = res.data[0];
              bpmUtils.executeTaskDetail(
                nar.processInstance, // processInstanceId
                nar.executionId, // executionId
                nar.dbid, // taskID
                nar.businessId, // formId
                nar.formResourceName, // formResourceName
                nar.processDefName || nar.taskTitle, // 展示流程标题
                nar.taskType, // formResourceName
                flowDetailUrl,
                bpmOperatorRefresh,
                viewCode,
                viewMode,
                customStatus,
                nar.microAppUrl
              );
            } else {
              const callback = (selectedTask) => {
                // 点击取消时，无已选待办
                if (selectedTask) {
                  const nar = selectedTask;
                  bpmUtils.executeTaskDetail(
                    nar.processInstance, // processInstanceId
                    nar.executionId, // executionId
                    nar.dbid, // taskID
                    nar.businessId, // formId
                    nar.formResourceName, // formResourceName
                    nar.processDefName || nar.taskTitle, // 展示流程标题
                    nar.taskType, // formResourceName
                    flowDetailUrl,
                    bpmOperatorRefresh,
                    viewCode,
                    viewMode,
                    customStatus,
                    nar.microAppUrl
                  );
                }
              };
              selectBpmTask({ taskList: res.data, callback });
            }
            resolve(res);
          } else {
            reject(res);
          }
        })
        .catch((err) => {
          resolve({ success: false, error: err });
        });
    });
    return promises;
  },
  executeTask: async (
    entryId,
    executionId,
    taskId,
    formId,
    url,
    title,
    taskType,
    flowDetailUrl,
    bpmOperatorRefresh,
    viewCode,
    viewMode,
    customStatus,
    siteType
  ) => {
    if (siteType === '1') {
      // 0 是主应用表单，1是子应用表单
      const postParam = {
        id: entryId || formId
      };
      getProcessDetailParameter(postParam)
        .then((res) => {
          if (res.success) {
            if (res.data.length === 0) {
              // eslint-disable-next-line eqeqeq
              if (viewMode == 'detailForm') {
                bpmUtils.executeTaskDetail(
                  entryId,
                  executionId,
                  taskId,
                  formId,
                  url,
                  title,
                  taskType,
                  flowDetailUrl,
                  bpmOperatorRefresh,
                  viewCode,
                  viewMode,
                  customStatus
                );
              } else {
                message.error('非本人参与流程，无法查看详情');
              }
            } else if (res.data.length > 0) {
              const value = res.data[0];
              bpmUtils.executeTaskDetail(
                value.processInstance, // processInstanceId
                value.executionId, // executionId
                value.dbid, // taskID
                value.businessId, // formId
                value.formResourceName, // formResourceName
                value.processDefName || value.taskTitle, // 展示流程标题
                value.taskType, // formResourceName
                flowDetailUrl,
                bpmOperatorRefresh,
                viewCode,
                viewMode,
                customStatus,
                value.microAppUrl
              );
            }
          }
        })
        // eslint-disable-next-line arrow-body-style
        .catch(() => {
          return;
        });
    } else {
      bpmUtils.executeTaskDetail(
        entryId,
        executionId,
        taskId,
        formId,
        url,
        title,
        taskType,
        flowDetailUrl,
        bpmOperatorRefresh,
        viewCode,
        viewMode,
        customStatus
      );
    }
  },
  executeTaskDetail: (
    entryId,
    executionId,
    taskId,
    formId,
    url,
    title,
    taskType,
    flowDetailUrl,
    bpmOperatorRefresh,
    viewCode,
    viewMode,
    customStatus,
    microAppUrl
  ) => {
    if (url == null || url === '') {
      return;
    }
    if (taskType === '1') {
      finishreader({
        dbid: taskId,
        entryId,
        doTask: 'false' // 是否是快速办理
      });
    }
    let proxyPage = 'N'; // 是否做页面代理
    if (url.indexOf('proxyPage=Y') !== -1) {
      // 是否做页面代理
      proxyPage = 'Y';
    }
    if (proxyPage !== 'Y') {
      // 不明确指定用代理页面的，则通通跳转到自己页面
      if (url.indexOf('?') > 0) {
        url += `&entryId=${entryId}`;
      } else {
        url += `?entryId=${entryId}`;
      }
      url += `&id=${formId}`;
      url += `&executionId=${executionId}`;
      if (url.indexOf('taskId=') === -1) {
        url += `&taskId=${taskId}`;
      }
      url += `&title=${title}`;
      // 电子表单流程详情页传递viewCode
      if (viewCode) {
        url += `&viewCode=${viewCode}`;
      }
      if (customStatus) {
        url += `&customStatus=${customStatus}`;
      }
      if (microAppUrl) {
        url += `&microAppUrl=${microAppUrl}`;
      }
      bpmUtils.openFlowDetail(url, flowDetailUrl, null, `FlowDetail-${entryId}-${executionId}`, viewMode);
    } else {
      // var redirectPath = "avicit/platform6/bpmreform/bpmbusiness/approve/ProcessApprove.jsp";
      // redirectPath += "?id=" + formId;
      // redirectPath += "&entryId=" + entryId;
      // redirectPath += "&executionId=" + executionId;
      // redirectPath += "&taskId=" + taskId;
      // redirectPath += "&title=" + encodeURI(title);
      if (url.indexOf('?') > 0) {
        url += `&entryId=${entryId}`;
      } else {
        url += `?entryId=${entryId}`;
      }
      url += `&id=${formId}`;
      url += `&executionId=${executionId}`;
      if (url.indexOf('taskId=') === -1) {
        url += `&taskId=${taskId}`;
      }
      url += `&title=${title}`;
      // redirectPath += "&url=" + encodeURI(url);
      // redirectPath += "&url=" + encodeURIComponent(url);
      // 电子表单流程详情页传递viewCode
      if (viewCode) {
        url += `&viewCode=${viewCode}`;
      }
      if (microAppUrl) {
        url += `&microAppUrl=${microAppUrl}`;
      }
      if (customStatus) {
        url += `&customStatus=${customStatus}`;
      }
      bpmUtils.openFlowDetail(url, flowDetailUrl, null, `FlowDetail-${entryId}-${executionId}`, viewMode);
    }
    if (bpmOperatorRefresh && typeof bpmOperatorRefresh === 'function') {
      window[`bpmOperatorRefresh${entryId}`] = () => {
        bpmOperatorRefresh();
      };
    }
  },
  // 获取流程 DefineId
  getDefineIdByEntryId: async (bpmInstanceObject, entryId) => {
    const result = await getDefineIdByEntryId({
      entryId
    });
    return new Promise((resolve) => {
      if (result.success) {
        bpmInstanceObject.bpmModel.defineId = result.data;
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },
  // 获取流程按钮设置
  // eslint-disable-next-line max-params
  getBpmButtonListBySelf: async (entryId, executionId, taskId, formId, userId) => {
    const result = await getBpmButtonListBySelf({
      bpm_entryId: entryId,
      bpm_executionId: executionId,
      bpm_taskId: taskId,
      bpm_formId: formId,
      userId
    });
    return new Promise((resolve) => {
      if (result.success) {
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },
  // 获取流程按钮
  // eslint-disable-next-line max-params
  getoperateright: async (bpmInstanceObject, entryId, executionId, taskId, formId) => {
    const result = await getoperateright({
      processInstanceId: entryId,
      executionId,
      taskId,
      bpm_formId: formId
    });
    return new Promise((resolve) => {
      if (result.success) {
        bpmInstanceObject.bpmModel.defineId = result.data.defineId;
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },

  // 请求合并后接口，获取defineId、流程按钮、流程节点、表单权限
  // eslint-disable-next-line max-params
  getOperaterightAndFormSecuritys: async (bpmInstanceObject, entryId, executionId, taskId, formId) => {
    const result = await getOperaterightAndFormSecuritys({
      processInstanceId: entryId,
      executionId,
      taskId,
      bpm_formId: formId
    });
    return new Promise((resolve) => {
      if (result.success) {
        bpmInstanceObject.bpmModel.defineId = result.data.defineId;
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },
  // 发起流程获取流程参数
  flowStart: async (defineId) => {
    const result = await start({
      defineId
    });
    return new Promise((resolve) => {
      if (result.success) {
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },

  // 获取流程节点信息
  setFlowStartAndEndTask: async (bpmInstanceObject, deploymentId) => {
    const res = await getProcessPicInfo({ deploymentId });
    let firstNode = [];
    const lastNode = [];
    if (res.success) {
      const startCell = res.data.processModel.node[0].id;
      const endCell = res.data.processModel.node[1].id;
      res.data.processModel.edge.forEach((item) => {
        if (item.source.cell === startCell) {
          firstNode = item.target.cell;
        }
        if (item.target.cell === endCell) {
          lastNode.push(item.target.cell);
        }
      });
      bpmInstanceObject.bpmModel.firstTaskName = firstNode;
      bpmInstanceObject.bpmModel.lastTaskName = lastNode;

      const result = { firstNode, lastNode };
      return new Promise((resolve) => {
        if (result.success) {
          resolve(result);
        } else {
          resolve(result);
        }
      });
    }
    return true;
  },
  // 获取流程页面标签页
  getBpmTabs: async () => {
    const result = await getBpmTabs('');
    return new Promise((resolve) => {
      if (result.success) {
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },
  // 获取流程标签类型
  getBpmNavbarType: async () => {
    const result = await getBpmNavbarType('');
    return new Promise((resolve) => {
      if (result.success) {
        resolve(result);
      } else {
        resolve(result);
      }
    });
  },
  // 设置标题
  setTitle: (title) => {
    if (title) {
      document.title = title;
    }
  },
  // 表单是否可编辑
  isEnable(bpmInstanceObject) {
    if ((bpmInstanceObject && !bpmInstanceObject.bpmSave) || bpmInstanceObject?.bpmSave?.enable === false) {
      return true; // 没有保存按钮 不可编辑
    }
    return false;
  },
  // form表单的是否可操作
  formOperability: (bpmInstanceObject, code) => {
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      return true;
    }
    if (bpmInstanceObject.bpmModel.userIdentityKey === '7' && bpmInstanceObject.bpmSave.enable) {
      // 管理员允许编辑表单
      return false;
    }
    // 非已办人、已阅人、读者、未知身份，判断表单字段权限
    if (
      code &&
      bpmInstanceObject.bpmEditor.formSecuritys.result &&
      bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0 &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '2' &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '4' &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '5' &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '0'
    ) {
      let result = true;
      bpmInstanceObject.bpmEditor.formSecuritys.result.forEach((tim) => {
        if (tim.tag === code && tim.operability === 1) {
          result = false;
        }
      });
      return result;
    }
    return true;
  },
  // form表单的是否必填
  /**
   * 增加代码生成器中，对数据库必填字段在拟稿节点的处理
   */
  formRequired: (bpmInstanceObject, code, options) => {
    // const tips = !options ? '请输入' : typeof options === 'object' ? options.tip || '请输入' : options;
    let tips = null;
    if (!options) {
      tips = '请输入';
    } else if (typeof options === 'object') {
      tips = options.tip || '请输入';
    } else {
      tips = options;
    }
    const dbRequired = !!(typeof options === 'object' && options.dbRequired);
    let result = [];
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      result = [];
    } else {
      if (code) {
        if (bpmInstanceObject.bpmEditor.formSecuritys.result && bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0) {
          bpmInstanceObject.bpmEditor.formSecuritys.result.forEach((tim) => {
            if (tim.tag === code && tim.required === 1 && tim.accessibility === 1 && tim.operability === 1) {
              result = [{ required: true, message: `${tips}${tim.tagName}！` }];
            }
          });
        }
      }
    }
    if (dbRequired && !result.length) {
      result = [{ required: true, message: '此项不可为空！' }];
    }
    return result;
  },
  // form表单的是否显示
  formAccessibility: (bpmInstanceObject, code) => {
    if (code) {
      if (bpmInstanceObject.bpmEditor.formSecuritys.result && bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0) {
        let result = false;
        bpmInstanceObject.bpmEditor.formSecuritys.result.forEach((tim) => {
          if (tim.tag === code && tim.accessibility === 1) {
            result = true;
          }
        });
        return result;
      }
      return true;
    }
    return true;
  },
  flowFileEditable: (...args) => bpmUtils.isDisabledAccessory(...args),
  // 附件是否可编辑
  isDisabledAccessory: (bpmInstanceObject, index) => {
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      return false;
    }
    if (bpmInstanceObject.bpmModel.userIdentityKey === '7' && bpmInstanceObject.bpmSave.enable) {
      // 管理员允许编辑附件
      return true;
    }
    if (
      bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths &&
      bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths.length >= index &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '2' &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '4' &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '5' &&
      bpmInstanceObject.bpmModel.userIdentityKey !== '0'
    ) {
      let result = false;
      if (
        bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths[index] &&
        bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths[index].operability === 1
      ) {
        result = true;
      }
      return result;
    }
    return false;
  },
  flowFileSecretChangeable: (...args) => bpmUtils.isLevelAccessory(...args),
  // 附件是否可修改密级
  isLevelAccessory: (bpmInstanceObject, index) => {
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      return false;
    }
    if (bpmInstanceObject.bpmModel.userIdentityKey === '7' && bpmInstanceObject.bpmSave.enable) {
      // 管理员允许修改附件密级
      return true;
    }
    if (
      bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths &&
      bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths.length >= index
    ) {
      let result = false;
      const attachment = bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths[index];
      if (attachment && attachment.modifySecretLevel === 1) {
        result = true;
      }
      return result;
    }
    return false;
  },

  // 子表状态控制
  // eslint-disable-next-line max-params
  subTablePermissions(bpmInstanceObject, pageSettingData, subTableName, buttonFors) {
    const { userIdentityKey } = bpmInstanceObject.bpmModel;
    let permissions = false;
    // 按钮控制
    if (buttonFors && buttonFors.length > 0) {
      if (bpmInstanceObject.bpmEditor.formSecuritys.result && bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0) {
        bpmInstanceObject.bpmEditor.formSecuritys.result.forEach((tim) => {
          if (buttonFors.indexOf(tim.tag) !== -1 && tim.accessibility === 1) {
            permissions = true;
          }
        });
      } else {
        permissions = false; // 未配置权限时 按钮不显示 by cuijj 2022-10-12
      }
    } else {
      permissions = true;
    }
    if (userIdentityKey !== '1' && userIdentityKey !== '6') {
      permissions = false;
    }
    // 已办 已阅 读者 未知 状态的隐藏子表按钮 禁用子表字段
    if (userIdentityKey === '2' || userIdentityKey === '4' || userIdentityKey === '5' || userIdentityKey === '0') {
      // 子表不可编辑
      pageSettingData.forEach((tim) => {
        tim.isCanEdit = false;
      });
      return { pageSettingData, permissions };
    }
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      // 子表不可编辑
      pageSettingData.forEach((tim) => {
        tim.isCanEdit = false;
      });
      return { pageSettingData, permissions };
    }
    if (bpmInstanceObject.bpmEditor.formSecuritys.result && bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0) {
      bpmInstanceObject.bpmEditor.formSecuritys.result.forEach((tim) => {
        pageSettingData.forEach((table, index) => {
          if (`${subTableName}__${table.dataIndex}` === tim.tag) {
            // 表格是否显示
            if (tim.accessibility !== 1) {
              pageSettingData.splice(index, 1);
            } else {
              // 表单是否可编辑
              if (tim.operability === 1) {
                table.isCanEdit = true;
              } else {
                table.isCanEdit = false;
              }
              // 是否必填
              if (tim.required === 1 && tim.accessibility === 1 && tim.operability === 1) {
                table.headerClassName = 'required-table-title';
              }
            }
          }
        });
      });
      return { pageSettingData, permissions };
    }
    return { pageSettingData, permissions };
  },
  /** 校验流程表单中的附件区域是否为必填项 */
  attachmentRequired: (bpmInstanceObject, fieldName) => {
    if (!bpmInstanceObject || !fieldName) {
      return [];
    }
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      return [];
    }
    if (bpmInstanceObject.bpmEditor?.formSecuritys?.attachmentAuths) {
      // eslint-disable-next-line max-len
      const attachmentFieldAuth = bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths.find(
        (item) => fieldName === item.tag && item.required === 1 && item.operability === 1
      );
      if (attachmentFieldAuth) {
        return [{ required: true, message: '请上传附件！' }];
      }
    }
    return [];
  },
  /** 校验流程表单中的附件区域是否显示 */
  attachmentAccessibility: (bpmInstanceObject, fieldName) => {
    if (!bpmInstanceObject || !fieldName) {
      return false;
    }
    if (bpmInstanceObject.bpmEditor?.formSecuritys?.attachmentAuths) {
      // 兼容历史数据，如果没有accessibility字段，则认为该字段是显示的
      const attachmentFieldAuth = bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths.some(
        (item) => fieldName === item.tag && item.accessibility === 0
      );
      return !attachmentFieldAuth;
    }
    return false;
  },
  // 附件是否必填
  isQequiredAccessory: (bpmInstanceObject, index) => {
    if (bpmUtils.isEnable(bpmInstanceObject)) {
      return [];
    }
    if (
      bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths &&
      bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths.length >= index
    ) {
      let result = [];
      const attachment = bpmInstanceObject.bpmEditor.formSecuritys.attachmentAuths[index];
      if (attachment && attachment.required === 1 && attachment.operability === 1) {
        result = [{ required: true, message: '请上传附件！' }];
      }
      return result;
    }
    return [];
  },
  /**
   * 创建空的xmlDocument
   * @param {*} arguments
   */
  createXmlDocument: () => {
    if (document.implementation && document.implementation.createDocument) {
      doc = document.implementation.createDocument('', '', null);
    } else if (window.ActiveXObject) {
      // eslint-disable-next-line no-undef
      doc = new ActiveXObject('Microsoft.XMLDOM');
    }
    return doc;
  },
  /**
   * 构建xml元素
   * @param {*} value
   */
  createElement: (value) => {
    if (doc == null) {
      doc = bpmUtils.createXmlDocument();
    }
    return doc.createElement(value);
  },

  createTextNode: (value) => {
    if (doc == null) {
      doc = bpmUtils.createXmlDocument();
    }
    if (value) {
      value = value.replace(/(^\s+)|(\s+$)/g, '');
    }
    return doc.createTextNode(value);
  },
  /**
   * 关闭当前窗口
   */
  closeWindow(entryId, viewCode) {
    if (bpmUtils.bpm_show_mode === 'dialog') {
      if (bpmUtils.bpm_modal_instance) {
        window.closeBpmDialog();
        window.closeBpmDialog = null;
        bpmUtils.bpm_modal_instance = null;
      }
    } else if (bpmUtils.bpm_show_mode === 'tab') {
      bpmUtils.close(router.currentRoute.value.path);
    } else {
      window.close();
    }

    this.refreshBack(entryId, viewCode); // 电子表单流程刷新父页面需要唯一视图编码标识
  },
  /**
   * 刷新父页面，bpm_operator_refresh
   */
  refreshBack(entryId, viewCode) {
    try {
      if (window.opener && !window.opener.closed) {
        // 优先根据entryId刷新父页面
        if (
          entryId &&
          window.opener[`bpmOperatorRefresh${entryId}`] &&
          typeof window.opener[`bpmOperatorRefresh${entryId}`] === 'function'
        ) {
          window.opener[`bpmOperatorRefresh${entryId}`]();
          setTimeout(() => {
            window.opener[`bpmOperatorRefresh${entryId}`] = null;
          }, 100);
        } else if (
          viewCode &&
          window.opener[`bpmOperatorRefresh${viewCode}`] &&
          typeof window.opener[`bpmOperatorRefresh${viewCode}`] === 'function'
        ) {
          // 电子表单流程刷新父页面需要唯一视图编码标识
          window.opener[`bpmOperatorRefresh${viewCode}`]();
          // setTimeout(() => {
          //   window.opener['bpmOperatorRefresh' + viewCode] = null;
          // }, 100);
        } else if (bpmUtils.notNull(window.opener.bpmOperatorRefresh)) {
          window.opener.bpmOperatorRefresh();
        }
      } else {
        // 弹框形式流程详情页 刷新外部界面 by cuijj 2021-08-13
        if (entryId && window[`bpmOperatorRefresh${entryId}`] && typeof window[`bpmOperatorRefresh${entryId}`] === 'function') {
          window[`bpmOperatorRefresh${entryId}`]();
          setTimeout(() => {
            window[`bpmOperatorRefresh${entryId}`] = null;
          }, 100);
        } else if (viewCode && window[`bpmOperatorRefresh${viewCode}`] && typeof window[`bpmOperatorRefresh${viewCode}`] === 'function') {
          // 电子表单流程刷新父页面需要唯一视图编码标识
          window[`bpmOperatorRefresh${viewCode}`]();
          // setTimeout(() => {
          //   window['bpmOperatorRefresh' + viewCode] = null;
          // }, 100);
        } else if (bpmUtils.notNull(window.bpmOperatorRefresh)) {
          window.bpmOperatorRefresh();
        }
      }
    } catch (e) {
      console.error(e);
    }
  },
  /**
   * 流程详情页面滚动事件
   */
  handleScroll() {
    const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop;
    let headTop = '0';
    let tabsTop = '0';
    if (scrollTop !== 0 && scrollTop - beterScrollTop > 0) {
      headTop = '-40px';
      tabsTop = '0px';
    } else {
      headTop = '0px';
      tabsTop = '40px';
    }
    beterScrollTop = scrollTop;
    return { headTop, tabsTop };
  }
};

export default bpmUtils;
function openBpmModal({ modalTitle, params, viewMode }) {
  const div = document.createElement('div');
  const el = document.createElement('div');
  document.body.appendChild(div);
  div.appendChild(el);
  let bpmModalIns = null;
  window.closeBpmDialog = () => {
    bpmModalIns?.unmount();
    bpmModalIns = null;
    div.parentNode && div.parentNode.removeChild(div);
  };
  const bpmModalProps = {
    ref: 'bpmModal',
    modalTitle,
    params,
    viewMode,
    onClose() {
      window.closeBpmDialog();
    }
  };
  bpmModalIns = Vue.createApp({
    render() {
      return Vue.h(AvicBpmModal, bpmModalProps);
    }
  });
  bpmModalIns.use(core);
  bpmModalIns.mount(el);
  return bpmModalIns;
}
export function selectBpmTask({ taskList, callback }) {
  const div = document.createElement('div');
  const el = document.createElement('div');
  document.body.appendChild(div);
  div.appendChild(el);
  let bpmTaskListIns = null;
  const selectBpmTaskProps = {
    ref: 'bpmTaskList',
    data: taskList,
    onClose() {
      bpmTaskListIns?.unmount();
      bpmTaskListIns = null;
      div.parentNode && div.parentNode.removeChild(div);
    },
    onSelect(args) {
      callback(args);
    }
  };
  bpmTaskListIns = Vue.createApp({
    render() {
      return Vue.h(AvicBpmDetailList, selectBpmTaskProps);
    },
    mounted() {
      this.$refs.bpmTaskList.showTaskList();
    }
  });
  bpmTaskListIns.use(core);
  bpmTaskListIns.mount(el);
}

/**
 * 根据表单编码及启动条件查询并选择可启动的流程定义
 * @param {formCode:String, formData: Object, callback: Function} param0
 * formCode 表单编码
 * formData 校验启动条件所需的参数
 * callback 回调函数
 */
export function startFlowByFormCode({ formCode, formData, callback }) {
  const div = document.createElement('div');
  const el = document.createElement('div');
  document.body.appendChild(div);
  div.appendChild(el);
  let bpmFormCodeIns = null;
  const formCodeProps = {
    ref: 'bpmFormCode',
    formCode,
    onClose() {
      bpmFormCodeIns?.unmount();
      bpmFormCodeIns = null;
      div.parentNode && div.parentNode.removeChild(div);
      callback(null);
    },
    onSelect(...args) {
      callback(...args);
    }
  };
  bpmFormCodeIns = Vue.createApp({
    render() {
      return Vue.h(AvicBpmFormCode, formCodeProps);
    },
    mounted() {
      this.$refs.bpmFormCode.loadFormcode({ formData });
    }
  });
  bpmFormCodeIns.use(core);
  bpmFormCodeIns.mount(el);
}

/* 打开流程详细页 */
export function openFlowDetail(bpmResult, bpmOperatorRefresh) {
  if (bpmResult.taskUrl) {
    let parameter = '';
    if (bpmResult.taskTitle) {
      parameter = `&title=${bpmResult.taskTitle}`;
    }
    bpmUtils.openFlowDetailByOptions({
      url: bpmResult.taskUrl + parameter,
      bpmOperatorRefresh,
      entryId: bpmResult.entryId
    });
  } else {
    message.error('打开流程详情失败！');
  }
}
/* 关闭流程loading */
export function closeFlowLoading(bpmInstanceObject) {
  if (bpmInstanceObject && bpmInstanceObject.bpmEditor) {
    bpmInstanceObject.bpmEditor.closeLoading(bpmInstanceObject);
    bpmInstanceObject.bpmEditor.setIsButtonLoading(bpmInstanceObject, false);
  }
}

/**
 * 构建字段权限结构
 * @param {Object} bpmInstanceObject
 * @returns
 */
export function getFieldAuth(bpmInstanceObject) {
  let tempJson = null; // 创建JSON
  if (
    bpmInstanceObject &&
    bpmInstanceObject.bpmEditor &&
    bpmInstanceObject.bpmEditor.formSecuritys.result &&
    bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0
  ) {
    tempJson = {}; // 创建JSON
    bpmInstanceObject.bpmEditor.formSecuritys.result.forEach((tim) => {
      // 处理单个字段的disabled属性
      let itemDisabled = true;
      if (tim.operability === 1) {
        itemDisabled = false;
      }
      // 处理单个字段的必填属性
      let itemRequired = {};
      if (tim.required === 1 && tim.accessibility === 1 && tim.operability === 1) {
        itemRequired = { required: true, message: `${tim.tagName}不可为空！` };
      }
      let itemVisible = false;
      if (tim.accessibility === 1) {
        itemVisible = true;
      }
      const fieldAuth = {
        visible: itemVisible,
        disabled: itemDisabled,
        required: itemRequired
      };
      tempJson[tim.tag] = fieldAuth;
    });
  }

  return tempJson;
}

export function getFieldVisible(authJson, fieldName) {
  let fieldVisiable = true;
  if (authJson != null && authJson[fieldName]) {
    fieldVisiable = authJson[fieldName].visible;
  }
  return fieldVisiable;
}

export function getFieldDisabled(authJson, fieldName, bpmInstanceObject) {
  if (!bpmInstanceObject) {
    return false;
  }
  if (bpmUtils.isEnable(bpmInstanceObject)) {
    return true;
  }
  if (bpmInstanceObject.bpmModel.userIdentityKey === '7' && bpmInstanceObject.bpmSave.enable) {
    // 管理员允许编辑表单
    return false;
  }
  if (
    fieldName &&
    bpmInstanceObject.bpmEditor.formSecuritys.result &&
    bpmInstanceObject.bpmEditor.formSecuritys.result.length > 0 &&
    bpmInstanceObject.bpmModel.userIdentityKey !== '2' &&
    bpmInstanceObject.bpmModel.userIdentityKey !== '4' &&
    bpmInstanceObject.bpmModel.userIdentityKey !== '5' &&
    bpmInstanceObject.bpmModel.userIdentityKey !== '0'
  ) {
    let fieldDisabled = true;
    if (authJson != null && authJson[fieldName]) {
      fieldDisabled = authJson[fieldName].disabled;
    }
    return fieldDisabled;
  }
  return true;
}

// eslint-disable-next-line max-params
// export function getFieldRequired(authJson, fieldName, rules, bpmInstanceObject, options) {
//   const dbRequired = !!(typeof options === 'object' && options.dbRequired);
//   let result = [];
//   const getRules = rules[fieldName];
//   if (getRules) {
//     result = cloneDeep(getRules);
//   }
//   // result.push({ required: true, message: `不能为空` })
//   if (!bpmUtils.isEnable(bpmInstanceObject)) {
//     if (authJson != null && authJson[fieldName]) {
//       result.push(authJson[fieldName].required);
//     }
//   }
//   if (dbRequired && !result.length) {
//     result.push({ required: true, message: '此项不可为空！' });
//   }
//   if (result.length > 0) {
//     result = result.filter((item) => JSON.stringify(item) !== '{}');
//   }
//   return result;
// }

export function getFieldRequired(authJson, fieldName, rules, bpmInstanceObject, options) {
  const dbRequired = options && typeof options === 'object' && options.dbRequired;
  let result = [];
  // 获取规则并深拷贝
  const getRules = rules[fieldName];
  if (getRules) {
    try {
      result = cloneDeep(getRules);
    } catch (error) {
      console.error('Failed to clone rules:', error);
      return [];
    }
  }

  // 如果 BPM 实例不可用
  if (!bpmUtils.isEnable(bpmInstanceObject)) {
    const fieldAuth = authJson?.[fieldName];
    const requiredConfig = fieldAuth?.required;
    if (fieldAuth && requiredConfig != null) {
      if (result.length) {
        // 如果结果中已经存在 required 属性，则更新它，否则添加
        result.forEach((item) => {
          if (item.required !== null) {
            item.required = requiredConfig.required ?? false;
          } else {
            result.push(requiredConfig);
          }
        });
      } else {
        result.push(requiredConfig);
      }
    }
  }
  // 如果数据库要求必填且结果为空
  if (dbRequired && !result.length) {
    result.push({ required: true, message: '此项不可为空！' });
  }
  // 过滤掉空对象
  result = result.filter((item) => Object.keys(item).length > 0);
  return result;
}
